package jwcb.admin.server.user.controller;

import jwcb.admin.api.user.domain.dto.AppUserListReqDTO;
import jwcb.admin.api.user.domain.dto.UserEditReqDTO;
import jwcb.admin.api.user.domain.vo.AppUserVo;
import jwcb.admin.api.user.feign.AppUserFeignClient;
import jwcb.admin.api.user.domain.dto.AppUserDTO;
import jwcb.admin.server.user.service.IAppUserService;
import jwcb.javapjframework.common.core.utils.BeanCopyUtil;
import jwcb.javapjframework.common.domain.domain.R;
import jwcb.javapjframework.common.domain.domain.dto.BasePageDTO;
import jwcb.javapjframework.common.domain.domain.vo.BasePageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app_user")
public class AppUserController implements AppUserFeignClient {


    @Autowired
    private IAppUserService appUserService;
    /**
     * 根据微信注册用户
     * @param openId 用户微信ID
     * @return C端用户VO
     */
    @Override
    public R<AppUserVo> registerByOpenId(String openId) {
        AppUserDTO appUserDto = appUserService.registerByOpenId(openId);
        AppUserVo appUserVo = new AppUserVo();
        BeanUtils.copyProperties(appUserDto,appUserVo);
        return R.ok(appUserVo);
    }


    /**
     * 根据OpenId查询用户
     * @param openId c端用户信息
     * @return 用户信息
     */
    @Override
    public R<AppUserVo> findByOpenId(String openId) {
        AppUserDTO appUserDTO = appUserService.findByOpenId(openId);
        if(appUserDTO == null) {
            return R.ok();
        }
        AppUserVo appUserVo = new AppUserVo();
        BeanUtils.copyProperties(appUserDTO,appUserVo);
        return R.ok(appUserVo);
    }


    /**
     * 根据手机号码注册用户
     * @param phone 手机号码
     * @return C端用户VO
     */
    @Override
    public R<AppUserVo> registerByPhone(String phone) {
        AppUserDTO appUserDTO = appUserService.registerByPhone(phone);
        AppUserVo appUserVo = new AppUserVo();
        BeanUtils.copyProperties(appUserDTO,appUserVo);
        return R.ok(appUserVo);
    }

    /**
     * 根据phone查询用户
     * @param phone c端用户信息
     * @return 用户信息
     */

    @Override
    public R<AppUserVo> findByPhone(String phone) {
        AppUserDTO appUserDTO = appUserService.findByPhone(phone);
        if(appUserDTO == null) {
            return R.ok();
        }
        AppUserVo appUserVo = new AppUserVo();
        BeanUtils.copyProperties(appUserDTO,appUserVo);
        return R.ok(appUserVo);
    }

    /**
     * 编辑C端用户
     * @param userEditReqDTO C端用户DTO
     * @return void
     */
    @Override
    public R<Void> edit(UserEditReqDTO userEditReqDTO) {
        appUserService.edit(userEditReqDTO);
        return R.ok();
    }



    /**
     * 根据用户ID获取用户信息
     * @param userId 用户ID
     * @return C端用户VO
     */
    @Override
    public R<AppUserVo> findById(Long userId) {
        AppUserDTO appUserDTO = appUserService.findById(userId);
        if (appUserDTO == null) {
            return R.ok();
        }
        AppUserVo appUserVo = new AppUserVo();
        BeanUtils.copyProperties(appUserDTO,appUserVo);
        return R.ok(appUserVo);
    }

    /**
     * 查询C端用户
     * @param appUserListReqDTO 查询C端用户DTO
     * @return C端用户分页结果
     */
    @PostMapping("/list/search")
    public R<BasePageVO<AppUserVo>> list(@RequestBody AppUserListReqDTO appUserListReqDTO) {
        BasePageDTO<AppUserDTO> appUserDTOList = appUserService.getUserList(appUserListReqDTO);
        BasePageVO<AppUserVo> result = new BasePageVO<>();
        BeanUtils.copyProperties(appUserDTOList, result);
        return R.ok(result);
    }



    /**
     * 根据用户ID列表获取用户列表信息
     * @param userIds 用户ID列表
     * @return C端用户VO列表
     */
    @Override
    public R<List<AppUserVo>> list(List<Long> userIds) {
        List<AppUserDTO> appUserDTOList = appUserService.getUserList(userIds);
        List<AppUserVo> appUserVos = BeanCopyUtil.copyListProperties(appUserDTOList, AppUserVo::new);
        return R.ok(appUserVos);
    }

}
